package com.hereway.core.util;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class ExtHelper {
	/**
	 * 通过List生成XML数据
	 * 
	 * @param recordTotal
	 *            记录总数，不一定与beanList中的记录数相等
	 * @param beanList
	 *            包含bean对象的集合
	 * @return 生成的XML数据
	 */
	public static String getXmlFromList(long recordCount, List beanList) {
		RecordCount count = new RecordCount(recordCount);
		List results = new ArrayList();
//		results.add(count);
		results.addAll(beanList);
		XStream sm = new XStream(new DomDriver());
		for (int i = 0; i < results.size(); i++) {
			Class c = results.get(i).getClass();
			String b = c.getName();
			String[] temp = b.split("\\.");
			sm.alias(temp[temp.length - 1], c);
		}
		String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
				+ "<count>" + recordCount + "</count>\n"
				+ sm.toXML(results);
		return xml;
	}

	/**
	 * 通过List生成XML数据
	 * 
	 * @param beanList
	 *            包含bean对象的集合
	 * @return 生成的XML数据
	 */
	public static String getXmlFromList(List beanList) {
		return getXmlFromList(beanList.size(), beanList);
	}

	/**
	 * 通过List生成JSON数据
	 * 
	 * @param recordTotal
	 *            记录总数，不一定与beanList中的记录数相等
	 * @param beanList
	 *            包含bean对象的集合
	 * @return 生成的JSON数据
	 */
	public static String getJsonFromList(long recordTotal, List beanList) {
		JsonResult total = new JsonResult();
		total.setTotal(recordTotal);
		total.setRecords(beanList);
		JSONObject o = new JSONObject(total);
		return o.toString();
	}

	/**
	 * 通过List生成JSON数据
	 * 
	 * @param beanList
	 *            包含bean对象的集合
	 * @return 生成的JSON数据
	 */
	public static String getJsonFromList(List beanList) {
		return getJsonFromList(beanList.size(), beanList);
	}

	/**
	 * 通过bean生成JSON数据
	 * 
	 * @param bean
	 *            bean对象
	 * @return 生成的JSON数据
	 */
	public static String getJsonFromBean(Object bean) {
		JSONObject o = new JSONObject(bean);
		return o.toString();
	}
}
